<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>

<c:catch var ="catchException">
    <fmt:parseDate var="data_inicio" value="${param.data_inicio}" pattern="dd/MM/yyyy"/>
    <fmt:parseDate var="data_fim" value="${param.data_fim}" pattern="dd/MM/yyyy"/>
    <c:choose>
        <c:when test="${param.oper=='edit'}">
            <c:choose>
                <c:when test="${param.copiar!='Nao'}">
                    <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
                        insert into Reservas
                        ([id_turma]
                        ,[ordem]
                        ,[id_componente]
                        ,[id_modulo]
                        ,[data_inicio]
                        ,[data_fim]
                        ,[id_sala]
                        ,[subturma],
                        [carga_horaria]
                        )

                        select id_turma, ordem, id_componente,id_modulo, data_inicio, data_fim, id_sala, ?, carga_horaria
                        from reservas
                        where id_reserva=?
                        <sql:param value="${param.subturma}"/>
                        <sql:param value="${param.id}"/>
                    </sql:update>
                </c:when>
                <c:otherwise>
                    <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
                        update reservas
                        set data_inicio=?,
                            data_fim=?,
                            id_sala=?,
                            ordem=?,
                            subturma=?, carga_horaria=?, id_regra_horarios=?
                        where id_reserva=?
                        <sql:dateParam value="${data_inicio}"/>
                        <sql:dateParam value="${data_fim}"/>
                        <sql:param value="${param.id_sala}"/>
                        <sql:param value="${param.ordem}"/>
                        <sql:param value="${param.subturma}"/>
                        <sql:param value="${param.carga_horaria}"/>
                        <sql:param value="${param.id_regra_horarios}"/>
                        <sql:param value="${param.id}"/>
                    </sql:update>
                </c:otherwise>
            </c:choose>

        </c:when>
        <c:when test="${param.oper=='del'}">
            <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
                delete from reservas
                where id_reserva=?
                <sql:param value="${param.id}"/>
            </sql:update>
        </c:when>
    </c:choose>
</c:catch>
<c:if test = "${catchException!=null}">
    Erro : Não foi possível realizar a alteração.<br><br>
    <p  style="color:red">${fn:substringAfter(catchException,"BD:")} </p>  
</c:if>

